package bingo.rpc.framework.cluster.impl;

import bingo.rpc.framework.cluster.ClusterStrategy;
import bingo.rpc.framework.helper.IPHelper;
import bingo.rpc.framework.model.ProviderService;

import java.util.List;

/**
 * 原地址hash负载均衡算法
 *
 * @author hujinsheng
 * @project bingo
 * @create 2017-12-14 14:58
 **/
public class HashClusterStrategyImpl implements ClusterStrategy {
    @Override
    public ProviderService select(List<ProviderService> providerServices) {
        //获取调用方ip
        String localIp = IPHelper.getLocalIp();
        //获取原地址的hashCode
        int hashCode = localIp.hashCode();
        //获取服务列表大小
        int size = providerServices.size();
        return providerServices.get(hashCode % size);

    }
}
